<?php 
class product_plugin_attributes extends abstract_product_plugin {

    public function getProductAttributes($product_id) {
        $attr_group_data = array();

        $attr_group_query = $this->pdo->prepare("SELECT ag.attribute_group_id, agd.name 
            FROM " . $this->db_prefix . "product_attribute pa 
            LEFT JOIN " . $this->db_prefix . "attribute a 
            ON (pa.attribute_id = a.attribute_id) 
            LEFT JOIN " . $this->db_prefix . "attribute_group ag 
            ON (a.attribute_group_id = ag.attribute_group_id) 
            LEFT JOIN " . $this->db_prefix . "attribute_group_description agd 
            ON (ag.attribute_group_id = agd.attribute_group_id) 
            WHERE pa.product_id = :product_id 
            AND agd.language_id = :language_id
            GROUP BY ag.attribute_group_id 
            ORDER BY ag.sort_order, agd.name");

        $attr_group_query->execute(array(
            ':product_id' => $product_id,
            ':language_id'=> $this->language_id
        ));

        foreach ($attr_group_query->fetchAll(PDO::FETCH_ASSOC) as $attr_group) {
            $attr_data = array();

            $attr_query = $this->pdo->prepare("SELECT a.attribute_id, ad.name, pa.text 
                FROM " . $this->db_prefix . "product_attribute pa 
                LEFT JOIN " . $this->db_prefix . "attribute a 
                ON (pa.attribute_id = a.attribute_id) 
                LEFT JOIN " . $this->db_prefix . "attribute_description ad 
                ON (a.attribute_id = ad.attribute_id) 
                WHERE pa.product_id = :product_id  
                AND a.attribute_group_id = :attribute_group_id
                AND ad.language_id = :language_id
                AND pa.language_id = :language_id 
                ORDER BY a.sort_order, ad.name");

            $attr_query->execute(array(
                ':product_id'           => $product_id,
                ':attribute_group_id'  => $attr_group['attribute_group_id'],
                ':language_id'          => $this->language_id
            ));

            foreach ($attr_query->fetchAll(PDO::FETCH_ASSOC) as $attr) {
                $attr_data[] = array(
                    'attribute_id' => $attr['attribute_id'],
                    'name'         => $attr['name'],
                    'text'         => $attr['text']		 	
                );
            }

            $attr_group_data[] = array(
                'attribute_group_id' => $attr_group['attribute_group_id'],
                'name'               => $attr_group['name'],
                'attribute'          => $attr_data
            );			
        }

        return $attr_group_data;
    }
}
